<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "stock_record".
 *
 * @property int $id
 * @property int $uid 用户ID
 * @property int $tally_id 所属账本
 * @property int $record_id 账单ID
 * @property int $house_id 仓库ID
 * @property string $house_title 仓库名称
 * @property string $product_detail 商品流转信息
 * @property string $total_money 产品金额
 * @property string $tally_money 账单金额
 * @property string $tally_date 账单日期
 * @property int $account_id 结算账户ID
 * @property string $account_name 结算账户
 * @property int $record_type 账单类型10收入20支出
 * @property int $sales_type 库存类型10销售出货20销售退货30采购进货40采购退货
 * @property int $state 1正常0待审-1失效
 * @property string $created_time 创建时间
 * @property string $updated_time 更新时间
 */
class StockRecord extends \yii\db\ActiveRecord
{

    /**
     * 定义库存账单的几种状态
     */
    const STATE_INVALID = 0; //状态等待审核
    const STATE_VALID   = 1; //状态正常
    const STATE_REFUSED = -1; //状态拒绝 审核未通过
    const STATE_DELETED = -2; //状态不可用 已删除

    /**
     * 定义库存账单的几种类型
     */
    const OPT_TYPE_SALE = 10;
    const OPT_TYPE_SALE_REFUND = 20;
    const OPT_TYPE_PURCHASE = 30;
    const OPT_TYPE_PURCHASE_REFUND = 40;

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'stock_record';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'tally_id', 'record_id', 'arap_id','house_id', 'account_id', 'category_id', 'store_id', 'member_id','record_type', 'sales_type', 'state'], 'integer'],
            [['product_detail'], 'required'],
            [['product_detail','memo'], 'string'],
            [['total_money', 'tally_money','total_num'], 'number'],
            [['created_time', 'updated_time'], 'safe'],
            [['house_title','store_name','member_name'], 'string'],
            [['tally_date'], 'string', 'max' => 10],
            [['account_name','category_name'], 'string', 'max' => 60],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'tally_id' => 'Tally ID',
            'record_id' => 'Record ID',
            'arap_id' => 'Arap ID', // 应收付ID
            'house_id' => 'House ID',
            'house_title' => 'House Title',
            'product_detail' => 'Product Detail',
            'total_money' => 'Total Money',
            'tally_money' => 'Tally Money',
            'tally_date' => 'Tally Date',
            'account_id' => 'Account ID',
            'account_name' => 'Account Name',
            'record_type' => 'Record Type',
            'sales_type' => 'Sales Type',
            'state' => 'State',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
        ];
    }

    /**
     * 获取状态的字符串表示
     * @param $state
     *
     * @return string
     */
    public static function getStateStr($state)
    {
        $str = '';
        switch ($state){
            case self::STATE_INVALID:
                $str = '待审核';
                break;
            case self::STATE_VALID:
                $str = '已入账';
                break;
            case self::STATE_REFUSED:
                $str = '已拒绝';
                break;
            case self::STATE_DELETED:
                $str = '已作废';
                break;
        }
        return $str;
    }

    /**
     * 获取操作类型字符串
     * @param $typeId
     *
     * @return mixed|string
     */
    public static function getOptStr($typeId)
    {
        $map = [
            10 => '销售出货',
            20 => '销售退货',
            30 => '采购进货',
            40 => '采购退货',
        ];
        if(isset($map[$typeId])) {
            return $map[$typeId];
        }else {
            return '';
        }
    }

    /**
     * 获取记录的收入、支出类型
     * @param $optType
     *
     * @return int
     */
    public static function getRecordInOutVal($optType)
    {
        if($optType == self::OPT_TYPE_SALE || $optType == self::OPT_TYPE_SALE_REFUND || $optType == self::OPT_TYPE_PURCHASE_REFUND) {
            return TallyRecord::TYPE_IN;
        }else {
            return TallyRecord::TYPE_OUT;
        }
    }
}
